package com.vhall.component.service.redpacket.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.vhall.component.dao.redpacket.RedPacketSettingMapper;
import com.vhall.component.entity.rebroadcast.RedPacketSettingEntity;
import com.vhall.component.entity.redpacket.vo.RedPacketSettingVO;
import com.vhall.component.plugin.core.service.PaasService;
import com.vhall.component.service.redpacket.common.RedpacketConstants;
import com.vhall.component.service.redpacket.service.RedPacketSettingService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author xuyongzheng
 * @since 2021/2/25 10:36
 **/
@Service
public class RedPacketSettingServiceImpl implements RedPacketSettingService {


    @Autowired
    private PaasService paasService;

    @Autowired
    private RedPacketSettingMapper mapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public RedPacketSettingEntity getRowByAppId(String appId) {
        if (StringUtils.isBlank(appId)) {
            return null;
        }
        String cacheKey = RedpacketConstants.APP_SETTING_BY_APP_ID + appId;
        String row = redisTemplate.opsForValue().get(cacheKey);
        if (row != null) {
            return JSON.parseObject(row, RedPacketSettingEntity.class);
        }
        RedPacketSettingEntity setting = selectByAppId(appId);
        if (setting != null) {
            redisTemplate.opsForValue().set(cacheKey, JSON.toJSONString(setting));
        }
        return setting;
    }

    @Override
    public RedPacketSettingEntity setting(RedPacketSettingVO vo) {
        RedPacketSettingEntity setting = new RedPacketSettingEntity();
        LambdaQueryWrapper<RedPacketSettingEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RedPacketSettingEntity::getAppId, paasService.getAppId());
        wrapper.last("limit 1");
        RedPacketSettingEntity record = mapper.selectOne(wrapper);
        if (record != null) {
            BeanUtils.copyProperties(vo, record);
            record.setUpdatedAt(LocalDateTime.now());
            mapper.updateById(record);

            String cacheKey = RedpacketConstants.APP_SETTING_BY_APP_ID + paasService.getAppId();
            redisTemplate.delete(cacheKey);
            return record;
        } else {
            BeanUtils.copyProperties(vo, setting);
            setting.setAppId(paasService.getAppId());
            setting.setPrivateKey(paasService.getAppSecret());
            setting.setCreatedAt(LocalDateTime.now());
            setting.setUpdatedAt(LocalDateTime.now());
            mapper.insert(setting);
        }
        return setting;
    }

    @Override
    public List<RedPacketSettingEntity> listAll() {
        LambdaQueryWrapper<RedPacketSettingEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RedPacketSettingEntity::getDeleted, RedpacketConstants.SOFT_DELETE);
        return mapper.selectList(wrapper);
    }


    private RedPacketSettingEntity selectByAppId(String appId) {

        QueryWrapper<RedPacketSettingEntity> where = new QueryWrapper<>();
        where.eq("app_id", appId);
        where.eq("deleted", RedpacketConstants.NOT_DELETED);
        where.last("limit 1;");
        return mapper.selectOne(where);
    }
}
